package com.wwwarehouse.common.http;

import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.BuildConfig;
import com.wwwarehouse.common.R;
import com.wwwarehouse.common.activity.base.BaseApplication;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.bean.upload.FileUploadBean;
import com.wwwarehouse.common.constant.Constant;
import com.wwwarehouse.common.eventbus_event.LogoutEvent;
import com.wwwarehouse.common.tools.BitmapUtil;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.common.tools.ThreadUtils;
import com.wwwarehouse.common.tools.rsa.StringUtils;
import com.yolanda.nohttp.Binary;
import com.yolanda.nohttp.FileBinary;
import com.yolanda.nohttp.Headers;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.OnUploadListener;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.download.DownloadListener;
import com.yolanda.nohttp.download.DownloadRequest;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;

import static com.wwwarehouse.common.activity.base.BaseApplication.getApplicationInstance;
import static com.wwwarehouse.common.activity.base.BaseApplication.sp;

/**
 * Created by Administrator on 2016/10/7.
 */

public class NoHttpUtils {
    private static String mBaseUrl = "";

    private static final int REQUEST_FAIL = -1;    //请求失败
    private static final int REQUEST_SUCCESS = 1;  //请求成功
    private static final int REQUEST_FINISH = 2;   //请求完成
    static FileUploadBean uploadBean = null;

    public static Request<byte[]> getByteRequest(String url, Map params, RequestMethod requestMethod) {
        // 获取当前环境， 如果没有就赋值为默认环境， 里面的sp获取默认环境，如果没有就默认cit，默认环境在Application里初始化
        String env = sp.getValue(Constant.sp_Env, sp.getValue(Constant.sp_EnvDefault, Constant.ENV_DEFAULT));

        if (Constant.ENV_DEV.equals(env)) {
            mBaseUrl = Constant.URL_BASE_DEV;
        } else if (Constant.ENV_CIT.equals(env)) {
            mBaseUrl = Constant.URL_BASE_CIT;
        } else if (Constant.ENV_SIT.equals(env)) {
            mBaseUrl = Constant.URL_BASE_SIT;
        } else if (Constant.ENV_UAT.equals(env)) {
            mBaseUrl = Constant.URL_BASE_UAT;
        } else if (Constant.ENV_RELEASE.equals(env)) {
            mBaseUrl = Constant.URL_BASE_RELEASE;
        } else if (Constant.ENV_DEV2.equals(env)) {
            mBaseUrl = Constant.URL_BASE_DEV2;
        } else if (Constant.ENV_CIT2.equals(env)) {
            mBaseUrl = Constant.URL_BASE_CIT2;
        }
        String jsonString = JSON.toJSONString(params);

        String urlString = mBaseUrl + url + "&data=" + jsonString;

        String uriString = "";

        try {
            URL urlObject = new URL(urlString);
            URI uriObject = new URI(urlObject.getProtocol(), urlObject.getHost() + ":" + urlObject.getPort(), urlObject.getPath(), urlObject.getQuery(), null);
            uriString = uriObject.toString();
            uriString = uriString.replace(":-1", "");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }

        LogUtils.showLog("请求的地址：" + uriString);

        // 创建请求对象
        Request<byte[]> request = NoHttp.createByteArrayRequest(uriString, requestMethod);

        //添加头信息
        String token = sp.getValue(Constant.sp_Token);

        //auth_token必须传，不能为""
        if (!TextUtils.isEmpty(token)) {
            request.addHeader("auth_token", token);
        } else {
            request.addHeader("auth_token", "10000");
        }

        request.addHeader("sign", "");
        request.addHeader("version", "1.0"); //服务端api版本
        request.addHeader("charset", "UTF-8");
        if (BuildConfig.AppName.equals(Constant.WANGCANG3)) {
            request.addHeader("w_appid", "wh_android_" + BaseApplication.getApplicationInstance().getVersionName());  //客户端版本描述
        } else if (BuildConfig.AppName.equals(Constant.VORES)) {
            request.addHeader("w_appid", "wh_android_vores_" + BaseApplication.getApplicationInstance().getVersionName());  //客户端版本描述
        }

        request.addHeader("deviceType", "a");
//        request.setDefineRequestBodyForJson(jsonString); // 添加参数
        request.setConnectTimeout(10 * 1000); // 设置连接超时
        request.setReadTimeout(20 * 1000); // 设置读取超时时间，也就是服务器的响应超时

        return request;
    }

    // 第二个参数取值RequestMethod.POST   RequestMethod.GET
    private static Request<String> getRequest(String url, Map params, RequestMethod requestMethod) {
        Map map = new HashMap();

        String versionName = BaseApplication.getApplicationInstance().getVersionName();

        if (requestMethod == RequestMethod.POST) {
            if (BuildConfig.AppName.equals(Constant.WANGCANG3)) {
                url = url + "&t=" + System.currentTimeMillis() + "&w_appid=" + "wh_android_" + versionName;
            } else if (BuildConfig.AppName.equals(Constant.VORES)) {
                url = url + "&t=" + System.currentTimeMillis() + "&w_appid=" + "wh_android_vores_" + versionName;
            }

            // 以map的形式添加请求参数
            map.put("data", params);
            map.put("notify_url", "");
            map.put("format", "json");
        } else if (requestMethod == RequestMethod.GET) {
            String jsonString = JSON.toJSONString(params);

            // 请求参数直接添加到url后面
            url = url + "?t=" + System.currentTimeMillis() + "&w_appid=" + "wh_android_" + versionName + "&data=" + jsonString;
            if (BuildConfig.AppName.equals(Constant.WANGCANG3)) {
                url = url + "?t=" + System.currentTimeMillis() + "&w_appid=" + "wh_android_" + versionName + "&data=" + jsonString;
            } else if (BuildConfig.AppName.equals(Constant.VORES)) {
                url = url + "?t=" + System.currentTimeMillis() + "&w_appid=" + "wh_android_vores_" + versionName + "&data=" + jsonString;
            }
        }

        String jsonString = JSON.toJSONString(map);
        LogUtils.showLog("请求的参数：" + jsonString);

        // 创建请求对象
        Request<String> request = NoHttp.createStringRequest(url, requestMethod);

        //添加头信息
        String token = sp.getValue(Constant.sp_Token);

        //auth_token必须传，不能为""，先随便写一个值
        if (!TextUtils.isEmpty(token)) {
            request.addHeader("auth_token", token);
        } else {
            request.addHeader("auth_token", "10000");
        }

        request.addHeader("sign", "");
        request.addHeader("version", "1.0"); //服务端api版本
        request.addHeader("charset", "UTF-8");
        if (BuildConfig.AppName.equals(Constant.WANGCANG3)) {
            request.addHeader("w_appid", "wh_android_" + BaseApplication.getApplicationInstance().getVersionName());  //客户端版本描述
        } else if (BuildConfig.AppName.equals(Constant.VORES)) {
            request.addHeader("w_appid", "wh_android_vores_" + BaseApplication.getApplicationInstance().getVersionName());  //客户端版本描述
        }
        request.addHeader("deviceType", "a");
        request.setDefineRequestBodyForJson(jsonString); // 添加参数
        request.setConnectTimeout(10 * 1000); // 设置连接超时
        request.setReadTimeout(20 * 1000); // 设置读取超时时间，也就是服务器的响应超时

        return request;
    }

    // 普通的Get Post请求的回调监听
    public interface OnResponseListener {
        void onSucceed(CommonClass commonClass, int what); // 请求成功回调

        void onFailed(String result, int what); // 请求失败回调

        void onFinish(int what);   // 请求完成回调
    }

    // 普通的Get Post请求的回调监听
    public interface OnDoubleResponseListener {
        void onSucceed(List<CommonClass> commonClassList, int what);  // 两个接口串行请求成功回调

        void onFailed(String result, int what); // 两个URL有一个请求失败回调，有一个失败就彻底失败了

        void onFinish(int what);   // 两个URL都请求完成回调

        void onFirstUrlSuccessed(CommonClass commonClass, int what); // 第一个URL调用成功回调
    }

    // 上传文件的监听
    public interface OnFileUploadListener {
        void onStart(int position); // 这个文件开始上传。

        void onCancel(int position); // 这个文件的上传被取消时。

        void onProgress(int position, int progress); // 这个文件的上传进度发生边耍

        void onFinish(int position); // 文件上传完成

        void onError(int position, Exception exception); // 文件上传发生错误。
    }

    // 全部文件上传完成后的回调
    public interface OnFileUploadResultListener {
        void onSucceed(int what, FileUploadBean uploadBean);

        void onFailed(int what, FileUploadBean uploadBean);
    }

    public interface OnFileDownloadListener {
        void onStart(int position, boolean isResume, long beforeLength, Headers headers, long allCount);

        void onDownloadError(int position, Exception exception);

        void onProgress(int position, int progress, long fileCount);

        void onFinish(int position, String filePath);

        void onCancel(int position);
    }

    // Get请求
    public static void httpGet(final String url, final Map params, final OnResponseListener responseListener, final int w) {
        // 获取当前环境， 如果没有就赋值为默认环境， 里面的sp获取默认环境，如果没有就默认cit，默认环境在Application里初始化
        String env = sp.getValue(Constant.sp_Env, sp.getValue(Constant.sp_EnvDefault, Constant.ENV_DEFAULT));

        if (Constant.ENV_DEV.equals(env)) {
            mBaseUrl = Constant.URL_BASE_DEV;
        } else if (Constant.ENV_CIT.equals(env)) {
            mBaseUrl = Constant.URL_BASE_CIT;
        } else if (Constant.ENV_SIT.equals(env)) {
            mBaseUrl = Constant.URL_BASE_SIT;
        } else if (Constant.ENV_UAT.equals(env)) {
            mBaseUrl = Constant.URL_BASE_UAT;
        } else if (Constant.ENV_RELEASE.equals(env)) {
            mBaseUrl = Constant.URL_BASE_RELEASE;
        } else if (Constant.ENV_DEV2.equals(env)) {
            mBaseUrl = Constant.URL_BASE_DEV2;
        } else if (Constant.ENV_CIT2.equals(env)) {
            mBaseUrl = Constant.URL_BASE_CIT2;
        }

        final String newUrl = mBaseUrl + url;
        LogUtils.showLog("请求的地址：" + newUrl);
        final Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                int what = msg.what;

                if (what == REQUEST_SUCCESS) {
                    // 请求成功
                    if (responseListener != null) {
                        String resultString = (String) msg.obj;

                        CommonClass commonClass = JSON.parseObject(resultString, CommonClass.class);

                        if (commonClass == null) {
                            responseListener.onFinish(w);
                            responseListener.onFailed("" + getApplicationInstance().getString(R.string.nohttp_service_down), w);
                            return;
                        }

                        if (commonClass != null && ("4".equals(commonClass.getCode()) || "2".equals(commonClass.getCode()))) {
                            EventBus.getDefault().post(new LogoutEvent("logout"));
                            return;
                        }

                        responseListener.onSucceed(commonClass, w);
                    }
                } else if (what == REQUEST_FINISH) {
                    // 请求完成
                    if (responseListener != null) {
                        responseListener.onFinish(w);
                    }
                } else {
                    if (responseListener != null) {
                        // 请求失败
                        responseListener.onFailed((String) msg.obj, w);
                    }
                }
            }
        };

        if (NetUtils.isHttpConnected(BaseApplication.getApplicationInstance())) {
            ThreadUtils.newThread(new Runnable() {
                @Override
                public void run() {
                    Request<String> request = getRequest(newUrl, params, RequestMethod.GET);
                    Response<String> response = NoHttp.startRequestSync(request);
                    String resultString = response.get();

                    LogUtils.showLog("请求的结果：" + resultString);

                    handler.sendEmptyMessage(REQUEST_FINISH); // 请求完成

                    if ((response.getHeaders().getResponseCode() + "").startsWith("2")) {
                        // 请求成功
                        Message msg = new Message();
                        msg.what = REQUEST_SUCCESS;
                        msg.obj = resultString;
                        handler.sendMessage(msg);
                    } else {
                        // 请求失败
                        Message msg = new Message();
                        msg.what = REQUEST_FAIL;
                        msg.obj = resultString;
                        handler.sendMessage(msg);
                    }
                }
            });
        } else {
            // 没联网，直接完成
            handler.sendEmptyMessage(REQUEST_FINISH); // 请求完成

            // 请求失败
            Message msg = new Message();
            msg.what = REQUEST_FAIL;
            msg.obj = "暂无网络连接";
            handler.sendMessage(msg);
        }
    }

    // Get请求
    public static void httpPost(final String url, final Map params, final OnResponseListener responseListener, final int w) {
        // 获取当前环境， 如果没有就赋值为默认环境， 里面的sp获取默认环境，如果没有就默认cit，默认环境在Application里初始化
        String env = sp.getValue(Constant.sp_Env, sp.getValue(Constant.sp_EnvDefault, Constant.ENV_DEFAULT));

        if (Constant.ENV_DEV.equals(env)) {
            mBaseUrl = Constant.URL_BASE_DEV;
        } else if (Constant.ENV_CIT.equals(env)) {
            mBaseUrl = Constant.URL_BASE_CIT;
        } else if (Constant.ENV_SIT.equals(env)) {
            mBaseUrl = Constant.URL_BASE_SIT;
        } else if (Constant.ENV_UAT.equals(env)) {
            mBaseUrl = Constant.URL_BASE_UAT;
        } else if (Constant.ENV_RELEASE.equals(env)) {
            mBaseUrl = Constant.URL_BASE_RELEASE;
        } else if (Constant.ENV_DEV2.equals(env)) {
            mBaseUrl = Constant.URL_BASE_DEV2;
        } else if (Constant.ENV_CIT2.equals(env)) {
            mBaseUrl = Constant.URL_BASE_CIT2;
        }
        final String newUrl = mBaseUrl + url;
        LogUtils.showLog("请求的地址：" + newUrl);
        final Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                int what = msg.what;

                if (what == REQUEST_SUCCESS) {
                    // 请求成功
                    if (responseListener != null) {
                        String resultString = (String) msg.obj;

                        CommonClass commonClass = JSON.parseObject(resultString, CommonClass.class);

                        if (commonClass == null) {
                            responseListener.onFinish(w);
                            responseListener.onFailed("" + getApplicationInstance().getString(R.string.nohttp_service_down), w);
                            return;
                        }

                        if (commonClass != null && ("4".equals(commonClass.getCode()) || "2".equals(commonClass.getCode()))) {
                            EventBus.getDefault().post(new LogoutEvent("logout"));
                            return;
                        }

                        responseListener.onSucceed(commonClass, w);


                    }
                } else if (what == REQUEST_FINISH) {
                    // 请求完成
                    if (responseListener != null) {
                        responseListener.onFinish(w);
                    }
                } else {
                    // 请求失败
                    if (responseListener != null) {
                        responseListener.onFailed((String) msg.obj, w);
                    }
                }
            }
        };

        if (NetUtils.isHttpConnected(BaseApplication.getApplicationInstance())) {
            ThreadUtils.newThread(new Runnable() {
                @Override
                public void run() {
                    Request<String> request = getRequest(newUrl, params, RequestMethod.POST);
                    Response<String> response = NoHttp.startRequestSync(request);
                    String resultString = response.get();

                    LogUtils.showLog("请求的结果：" + resultString);

                    handler.sendEmptyMessage(REQUEST_FINISH); // 请求完成

                    if ((response.getHeaders().getResponseCode() + "").startsWith("2")) {
                        // 请求成功
                        Message msg = new Message();
                        msg.what = REQUEST_SUCCESS;
                        msg.obj = resultString;
                        handler.sendMessage(msg);
                    } else {
                        // 请求失败
                        Message msg = new Message();
                        msg.what = REQUEST_FAIL;
                        msg.obj = resultString;
                        handler.sendMessage(msg);
                    }
                }
            });
        } else {
            // 没联网，直接完成
            handler.sendEmptyMessage(REQUEST_FINISH); // 请求完成

            // 请求失败
            Message msg = new Message();
            msg.what = REQUEST_FAIL;
            msg.obj = "暂无网络连接";
            handler.sendMessage(msg);
        }
    }

    // 请求两个接口串行请求,第一个参数是size为2的URL列表，第二个参数是两个URL的参数
    public static void httpDoublePost(final List urlList, final List<Map> paramsList, final OnDoubleResponseListener responseListener, final int w) {
        final ArrayList<CommonClass> dataList = new ArrayList<>();
        // 获取当前环境， 如果没有就赋值为默认环境， 里面的sp获取默认环境，如果没有就默认cit，默认环境在Application里初始化
        String env = sp.getValue(Constant.sp_Env, sp.getValue(Constant.sp_EnvDefault, Constant.ENV_DEFAULT));

        if (Constant.ENV_DEV.equals(env)) {
            mBaseUrl = Constant.URL_BASE_DEV;
        } else if (Constant.ENV_CIT.equals(env)) {
            mBaseUrl = Constant.URL_BASE_CIT;
        } else if (Constant.ENV_SIT.equals(env)) {
            mBaseUrl = Constant.URL_BASE_SIT;
        } else if (Constant.ENV_UAT.equals(env)) {
            mBaseUrl = Constant.URL_BASE_UAT;
        } else if (Constant.ENV_DEV2.equals(env)) {
            mBaseUrl = Constant.URL_BASE_DEV2;
        } else if (Constant.ENV_CIT2.equals(env)) {
            mBaseUrl = Constant.URL_BASE_CIT2;
        } else if (Constant.ENV_RELEASE.equals(env)) {
            mBaseUrl = Constant.URL_BASE_RELEASE;
        }

        // 请求第一个接口
        final String newUrl = mBaseUrl + urlList.get(0);

        final Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                int what = msg.what;

                if (what == REQUEST_SUCCESS) {
                    // 第一个接口请求成功
                    String resultString = (String) msg.obj;
                    CommonClass commonClass = JSON.parseObject(resultString, CommonClass.class);

                    if (commonClass == null) {
                        responseListener.onFinish(w);
                        responseListener.onFailed("" + getApplicationInstance().getString(R.string.nohttp_service_down), w);
                        return;
                    }

                    if (commonClass != null && ("4".equals(commonClass.getCode()) || "2".equals(commonClass.getCode()))) {
                        EventBus.getDefault().post(new LogoutEvent("logout"));
                        return;
                    }

                    dataList.add(commonClass);

                    // 请求第二个接口
                    final String newUrl2 = mBaseUrl + urlList.get(1);

                    final Handler handler2 = new Handler() {
                        @Override
                        public void handleMessage(Message msg) {
                            int what = msg.what;

                            if (what == REQUEST_SUCCESS) {
                                // 请求成功
                                if (responseListener != null) {
                                    String resultString = (String) msg.obj;

                                    CommonClass commonClass = JSON.parseObject(resultString, CommonClass.class);
                                    dataList.add(commonClass);
                                    if ("4".equals(commonClass.getCode()) || "2".equals(commonClass.getCode())) {
                                        EventBus.getDefault().post(new LogoutEvent("logout"));
                                        return;
                                    }

                                    if (responseListener != null) {
                                        responseListener.onSucceed(dataList, w);
                                        responseListener.onFinish(w);
                                    }
                                }
                            } else if (what == REQUEST_FINISH) {
                                // 请求完成
                                if (responseListener != null) {
                                    responseListener.onFinish(w);
                                }
                            } else {
                                // 请求失败
                                if (responseListener != null) {
                                    responseListener.onFailed((String) msg.obj, w);
                                }
                            }
                        }
                    };

                    if (NetUtils.isHttpConnected(BaseApplication.getApplicationInstance())) {
                        ThreadUtils.newThread(new Runnable() {
                            @Override
                            public void run() {
                                Request<String> request = getRequest(newUrl2, paramsList.get(1), RequestMethod.POST);
                                Response<String> response = NoHttp.startRequestSync(request);
                                String resultString = response.get();

                                LogUtils.showLog("请求的结果：" + resultString);

                                handler2.sendEmptyMessage(REQUEST_FINISH); // 请求完成

                                if ((response.getHeaders().getResponseCode() + "").startsWith("2")) {
                                    // 请求成功
                                    Message msg = new Message();
                                    msg.what = REQUEST_SUCCESS;
                                    msg.obj = resultString;
                                    handler2.sendMessage(msg);
                                } else {
                                    // 请求失败
                                    Message msg = new Message();
                                    msg.what = REQUEST_FAIL;
                                    msg.obj = resultString;
                                    handler2.sendMessage(msg);
                                }
                            }
                        });
                    } else {
                        // 没联网，直接完成
                        handler2.sendEmptyMessage(REQUEST_FINISH); // 请求完成

                        // 请求失败
                        Message msg2 = new Message();
                        msg2.what = REQUEST_FAIL;
                        msg2.obj = "暂无网络连接";
                        handler2.sendMessage(msg2);
                    }
                } else {
                    // 请求失败
                    if (responseListener != null) {
                        responseListener.onFailed((String) msg.obj, w);
                    }

                    // 请求完成
                    if (responseListener != null) {
                        responseListener.onFinish(w);
                    }
                }
            }
        };

        if (NetUtils.isHttpConnected(BaseApplication.getApplicationInstance())) {
            ThreadUtils.newThread(new Runnable() {
                @Override
                public void run() {
                    Request<String> request = getRequest(newUrl, paramsList.get(0), RequestMethod.POST);
                    Response<String> response = NoHttp.startRequestSync(request);
                    String resultString = response.get();

                    LogUtils.showLog("请求的结果：" + resultString);
                    if ((response.getHeaders().getResponseCode() + "").startsWith("2")) {
                        // 请求成功
                        Message msg = new Message();
                        msg.what = REQUEST_SUCCESS;
                        msg.obj = resultString;
                        handler.sendMessage(msg);
                    } else {
                        // 请求失败
                        Message msg = new Message();
                        msg.what = REQUEST_FAIL;
                        msg.obj = resultString;
                        handler.sendMessage(msg);
                    }
                }
            });
        } else {
            // 没联网，直接完成
            handler.sendEmptyMessage(REQUEST_FINISH); // 请求完成

            // 请求失败
            Message msg = new Message();
            msg.what = REQUEST_FAIL;
            msg.obj = "暂无网络连接";
            handler.sendMessage(msg);
        }
    }

    // 请求两个接口串行请求,第一个参数是size为2的URL列表，第二个参数是第一个URL的参数，第三个参数是第二个URL的参数，要在第一个请求回调中赋值
    public static void httpDoublePost(final List urlList, final Map<String, Object> firstUrlParams, final Map<String, Object> secondUrlParams, final OnDoubleResponseListener responseListener, final int w) {
        final ArrayList<CommonClass> dataList = new ArrayList<>();
        // 获取当前环境， 如果没有就赋值为默认环境， 里面的sp获取默认环境，如果没有就默认cit，默认环境在Application里初始化
        String env = sp.getValue(Constant.sp_Env, sp.getValue(Constant.sp_EnvDefault, Constant.ENV_DEFAULT));

        if (Constant.ENV_DEV.equals(env)) {
            mBaseUrl = Constant.URL_BASE_DEV;
        } else if (Constant.ENV_CIT.equals(env)) {
            mBaseUrl = Constant.URL_BASE_CIT;
        } else if (Constant.ENV_SIT.equals(env)) {
            mBaseUrl = Constant.URL_BASE_SIT;
        } else if (Constant.ENV_UAT.equals(env)) {
            mBaseUrl = Constant.URL_BASE_UAT;
        } else if (Constant.ENV_DEV2.equals(env)) {
            mBaseUrl = Constant.URL_BASE_DEV2;
        } else if (Constant.ENV_CIT2.equals(env)) {
            mBaseUrl = Constant.URL_BASE_CIT2;
        } else if (Constant.ENV_RELEASE.equals(env)) {
            mBaseUrl = Constant.URL_BASE_RELEASE;
        }
//        else if (Constant.ENV_DEVOPS.equals(env)) {
//            mBaseUrl = Constant.URL_BASE_DEVOPS;
//        }

        // 请求第一个接口
        final String newUrl = mBaseUrl + urlList.get(0);

        final Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                int what = msg.what;

                if (what == REQUEST_SUCCESS) {
                    // 第一个接口请求成功
                    String resultString = (String) msg.obj;
                    CommonClass commonClass = JSON.parseObject(resultString, CommonClass.class);
                    if ("4".equals(commonClass.getCode()) || "2".equals(commonClass.getCode())) {
                        EventBus.getDefault().post(new LogoutEvent("logout"));
                        return;
                    }

                    dataList.add(commonClass);

                    if (responseListener != null) {
                        responseListener.onFirstUrlSuccessed(commonClass, what);
                    }

                    // 请求第二个接口
                    final String newUrl2 = mBaseUrl + urlList.get(1);
                    final Handler handler2 = new Handler() {
                        @Override
                        public void handleMessage(Message msg) {
                            int what = msg.what;

                            if (what == REQUEST_SUCCESS) {
                                // 请求成功
                                if (responseListener != null) {
                                    String resultString = (String) msg.obj;

                                    CommonClass commonClass = JSON.parseObject(resultString, CommonClass.class);
                                    dataList.add(commonClass);
                                    if ("4".equals(commonClass.getCode()) || "2".equals(commonClass.getCode())) {
                                        EventBus.getDefault().post(new LogoutEvent("logout"));
                                        return;
                                    }

                                    if (responseListener != null) {
                                        responseListener.onSucceed(dataList, w);
                                        responseListener.onFinish(w);
                                    }
                                }
                            } else if (what == REQUEST_FINISH) {
                                // 请求完成
                                if (responseListener != null) {
                                    responseListener.onFinish(w);
                                }
                            } else {
                                // 请求失败
                                if (responseListener != null) {
                                    responseListener.onFailed((String) msg.obj, w);
                                }
                            }
                        }
                    };

                    if (NetUtils.isHttpConnected(BaseApplication.getApplicationInstance())) {
                        ThreadUtils.newThread(new Runnable() {
                            @Override
                            public void run() {
                                Request<String> request = getRequest(newUrl2, secondUrlParams, RequestMethod.POST);
                                Response<String> response = NoHttp.startRequestSync(request);
                                String resultString = response.get();

                                LogUtils.showLog("请求的结果：" + resultString);

                                handler2.sendEmptyMessage(REQUEST_FINISH); // 请求完成

                                if ((response.getHeaders().getResponseCode() + "").startsWith("2")) {
                                    // 请求成功
                                    Message msg = new Message();
                                    msg.what = REQUEST_SUCCESS;
                                    msg.obj = resultString;
                                    handler2.sendMessage(msg);
                                } else {
                                    // 请求失败
                                    Message msg = new Message();
                                    msg.what = REQUEST_FAIL;
                                    msg.obj = resultString;
                                    handler2.sendMessage(msg);
                                }
                            }
                        });
                    } else {
                        // 没联网，直接完成
                        handler2.sendEmptyMessage(REQUEST_FINISH); // 请求完成

                        // 请求失败
                        Message msg2 = new Message();
                        msg2.what = REQUEST_FAIL;
                        msg2.obj = "暂无网络连接";
                        handler2.sendMessage(msg2);
                    }
                } else {
                    // 请求失败
                    if (responseListener != null) {
                        responseListener.onFailed((String) msg.obj, w);
                    }

                    // 请求完成
                    if (responseListener != null) {
                        responseListener.onFinish(w);
                    }
                }
            }
        };

        if (NetUtils.isHttpConnected(BaseApplication.getApplicationInstance())) {
            ThreadUtils.newThread(new Runnable() {
                @Override
                public void run() {
                    Request<String> request = getRequest(newUrl, firstUrlParams, RequestMethod.POST);
                    Response<String> response = NoHttp.startRequestSync(request);
                    String resultString = response.get();

                    LogUtils.showLog("请求的结果：" + resultString);
                    if ((response.getHeaders().getResponseCode() + "").startsWith("2")) {
                        // 请求成功
                        Message msg = new Message();
                        msg.what = REQUEST_SUCCESS;
                        msg.obj = resultString;
                        handler.sendMessage(msg);
                    } else {
                        // 请求失败
                        Message msg = new Message();
                        msg.what = REQUEST_FAIL;
                        msg.obj = resultString;
                        handler.sendMessage(msg);
                    }
                }
            });
        } else {
            // 没联网，直接完成
            handler.sendEmptyMessage(REQUEST_FINISH); // 请求完成

            // 请求失败
            Message msg = new Message();
            msg.what = REQUEST_FAIL;
            msg.obj = "暂无网络连接";
            handler.sendMessage(msg);
        }
    }

    // 组拼get参数, 不过没用上
    private static String getUrl(String url, HashMap<String, String> params) {
        // 添加url参数
        if (params != null) {
            Iterator<String> it = params.keySet().iterator();

            StringBuffer stringBuffer = null;

            while (it.hasNext()) {
                String key = it.next();
                String value = params.get(key);

                if (stringBuffer == null) {
                    stringBuffer = new StringBuffer();
                    stringBuffer.append("?");
                } else {
                    stringBuffer.append("&");
                }

                stringBuffer.append(key);
                stringBuffer.append("=");
                stringBuffer.append(value);
            }

            url += stringBuffer.toString();
        }

        return url;
    }

    // 文件上传
    private static OnFileUploadListener mOnFileUploadListener;

    public static void httpUploadFileRequest(String url, List<String> fileList, OnFileUploadListener onFileUploadListener, final OnFileUploadResultListener onFileUploadResultListener) {
        mOnFileUploadListener = onFileUploadListener;
        // 获取当前环境， 如果没有就赋值为默认环境， 里面的sp获取默认环境，如果没有就默认cit，默认环境在Application里初始化
        String env = sp.getValue(Constant.sp_Env, sp.getValue(Constant.sp_EnvDefault, Constant.ENV_DEFAULT));

        if (Constant.ENV_DEV.equals(env)) {
            mBaseUrl = Constant.URL_BASE_DEV;
        } else if (Constant.ENV_CIT.equals(env)) {
            mBaseUrl = Constant.URL_BASE_CIT;
        } else if (Constant.ENV_SIT.equals(env)) {
            mBaseUrl = Constant.URL_BASE_SIT;
        } else if (Constant.ENV_UAT.equals(env)) {
            mBaseUrl = Constant.URL_BASE_UAT;
        } else if (Constant.ENV_DEV2.equals(env)) {
            mBaseUrl = Constant.URL_BASE_DEV2;
        } else if (Constant.ENV_CIT2.equals(env)) {
            mBaseUrl = Constant.URL_BASE_CIT2;
        } else if (Constant.ENV_RELEASE.equals(env)) {
            mBaseUrl = Constant.URL_BASE_RELEASE;
        }

        final String newUrl = mBaseUrl + url;
        LogUtils.showLog("请求的地址：" + newUrl);
        final Request<String> request = NoHttp.createStringRequest(newUrl, RequestMethod.POST);
        //添加头信息
        String token = sp.getValue(Constant.sp_Token);

        //auth_token必须传，不能为""
        if (!TextUtils.isEmpty(token)) {
            request.addHeader("auth_token", token);
        } else {
            request.addHeader("auth_token", "10000");
        }
        request.addHeader("deviceType", "a");

//            for (int i = 0; i < fileList.size(); i++) {
//                File file = new File(fileList.get(i));
//                BasicBinary binary = new FileBinary(file);
//                binary.setUploadListener(i, mOnUploadListener);
//                request.add("file", binary);
//            }

//        ArrayList<String> tempPathList = new ArrayList<>();
//        for (int i = 0; i < fileList.size(); i++) {
//            if (fileList.get(i).endsWith("png")
//                    || fileList.get(i).endsWith("jpg")
//                    || fileList.get(i).endsWith("jepg")
//                    || fileList.get(i).endsWith("JEPG")) {
//                fileList.set(i, BitmapUtil.getSmallImage(fileList.get(i)));
//            }
//        }

        List<Binary> binaries = new ArrayList<>();
        for (int i = 0; i < fileList.size(); i++) {
            File file = new File(fileList.get(i));
            FileBinary binary = new FileBinary(file);
            binary.setUploadListener(i, mOnUploadListener);
            binaries.add(binary);
        }
//            for (int i = 0; i < fileList.size(); i++) {
//                File file = new File(fileList.get(i));
//                Bitmap bmp = BitmapFactory.decodeFile(fileList.get(i));
//                BitmapBinary binary = new BitmapBinary(bmp, file.getName());
//                binary.setUploadListener(i, mOnUploadListener);
//                binaries.add(binary);
//            }
        request.add("file", binaries);

        CallServer.getRequestInstance().add(0, request, new HttpListener<String>() {
            @Override
            public void onSucceed(int what, Response<String> response) {
                if ((response.getHeaders().getResponseCode() + "").startsWith("2")) {
                    String resultString = response.get();

                    LogUtils.showLog("请求的结果：" + resultString);

                    uploadBean = JSON.parseObject(resultString, FileUploadBean.class);

                    if ("4".equals(uploadBean.getCode())) {

                        return;
                    }

                    if (onFileUploadResultListener != null) {
                        onFileUploadResultListener.onSucceed(what, uploadBean);
                    }
                } else {
                    uploadBean = JSON.parseObject(response.get(), FileUploadBean.class);
                    if (onFileUploadResultListener != null) {
                        onFileUploadResultListener.onFailed(what, uploadBean);
                    }
                }
            }

            @Override
            public void onFailed(int what, Response<String> response) {
                String resultString = response.get();
                LogUtils.showLog("请求的结果：" + resultString);
                if (onFileUploadResultListener != null) {
                    uploadBean = JSON.parseObject(response.get(), FileUploadBean.class);
                    onFileUploadResultListener.onFailed(what, uploadBean);
                }
            }
        });
    }

    public static void httpUploadImgRequest(String url, List<String> fileList, OnFileUploadListener onFileUploadListener, final OnFileUploadResultListener onFileUploadResultListener) {
        mOnFileUploadListener = onFileUploadListener;
        // 获取当前环境， 如果没有就赋值为默认环境， 里面的sp获取默认环境，如果没有就默认cit，默认环境在Application里初始化
        String env = sp.getValue(Constant.sp_Env, sp.getValue(Constant.sp_EnvDefault, Constant.ENV_DEFAULT));

        if (Constant.ENV_DEV.equals(env)) {
            mBaseUrl = Constant.URL_BASE_DEV;
        } else if (Constant.ENV_CIT.equals(env)) {
            mBaseUrl = Constant.URL_BASE_CIT;
        } else if (Constant.ENV_SIT.equals(env)) {
            mBaseUrl = Constant.URL_BASE_SIT;
        } else if (Constant.ENV_UAT.equals(env)) {
            mBaseUrl = Constant.URL_BASE_UAT;
        } else if (Constant.ENV_DEV2.equals(env)) {
            mBaseUrl = Constant.URL_BASE_DEV2;
        } else if (Constant.ENV_CIT2.equals(env)) {
            mBaseUrl = Constant.URL_BASE_CIT2;
        } else if (Constant.ENV_RELEASE.equals(env)) {
            mBaseUrl = Constant.URL_BASE_RELEASE;
        }

        final String newUrl = mBaseUrl + url;
        LogUtils.showLog("请求的地址：" + newUrl);
        final Request<String> request = NoHttp.createStringRequest(newUrl, RequestMethod.POST);
        //添加头信息
        String token = sp.getValue(Constant.sp_Token);

        //auth_token必须传，不能为""
        if (!TextUtils.isEmpty(token)) {
            request.addHeader("auth_token", token);
        } else {
            request.addHeader("auth_token", "10000");
        }
        request.addHeader("deviceType", "a");

//            for (int i = 0; i < fileList.size(); i++) {
//                File file = new File(fileList.get(i));
//                BasicBinary binary = new FileBinary(file);
//                binary.setUploadListener(i, mOnUploadListener);
//                request.add("file", binary);
//            }

//        ArrayList<String> tempPathList = new ArrayList<>();
//        for (int i = 0; i < fileList.size(); i++) {
//            if (fileList.get(i).endsWith("png")
//                    || fileList.get(i).endsWith("jpg")
//                    || fileList.get(i).endsWith("jepg")
//                    || fileList.get(i).endsWith("JEPG")) {
//                fileList.set(i, BitmapUtil.getSmallImage(fileList.get(i)));
//            }
//        }

        for (int i = 0; i < fileList.size(); i++) {
            String result = BitmapUtil.getSmallImage(fileList.get(i));
            fileList.set(i, result);
        }
        List<Binary> binaries = new ArrayList<>();
        for (int i = 0; i < fileList.size(); i++) {
            File file = new File(fileList.get(i));
            FileBinary binary = new FileBinary(file);
            binary.setUploadListener(i, mOnUploadListener);
            binaries.add(binary);
        }
//            for (int i = 0; i < fileList.size(); i++) {
//                File file = new File(fileList.get(i));
//                Bitmap bmp = BitmapFactory.decodeFile(fileList.get(i));
//                BitmapBinary binary = new BitmapBinary(bmp, file.getName());
//                binary.setUploadListener(i, mOnUploadListener);
//                binaries.add(binary);
//            }
        request.add("file", binaries);

        CallServer.getRequestInstance().add(0, request, new HttpListener<String>() {
            @Override
            public void onSucceed(int what, Response<String> response) {
                if ((response.getHeaders().getResponseCode() + "").startsWith("2")) {
                    String resultString = response.get();

                    LogUtils.showLog("请求的结果：" + resultString);

                    uploadBean = JSON.parseObject(resultString, FileUploadBean.class);

                    if ("4".equals(uploadBean.getCode())) {

                        return;
                    }

                    if (onFileUploadResultListener != null) {
                        onFileUploadResultListener.onSucceed(what, uploadBean);
                    }
                } else {
                    uploadBean = JSON.parseObject(response.get(), FileUploadBean.class);
                    if (onFileUploadResultListener != null) {
                        onFileUploadResultListener.onFailed(what, uploadBean);
                    }
                }
            }

            @Override
            public void onFailed(int what, Response<String> response) {
                String resultString = response.get();
                LogUtils.showLog("请求的结果：" + resultString);
                if (onFileUploadResultListener != null) {
                    uploadBean = JSON.parseObject(response.get(), FileUploadBean.class);
                    onFileUploadResultListener.onFailed(what, uploadBean);
                }
            }
        });
    }

    public static List<Binary> httpUploadFileRequest(int what, String url, Map<Integer, String> fileMap, OnFileUploadListener onFileUploadListener, final OnFileUploadResultListener onFileUploadResultListener) {
        mOnFileUploadListener = onFileUploadListener;
        // 获取当前环境， 如果没有就赋值为默认环境， 里面的sp获取默认环境，如果没有就默认cit，默认环境在Application里初始化
        String env = sp.getValue(Constant.sp_Env, sp.getValue(Constant.sp_EnvDefault, Constant.ENV_DEFAULT));

        if (Constant.ENV_DEV.equals(env)) {
            mBaseUrl = Constant.URL_BASE_DEV;
        } else if (Constant.ENV_CIT.equals(env)) {
            mBaseUrl = Constant.URL_BASE_CIT;
        } else if (Constant.ENV_SIT.equals(env)) {
            mBaseUrl = Constant.URL_BASE_SIT;
        } else if (Constant.ENV_UAT.equals(env)) {
            mBaseUrl = Constant.URL_BASE_UAT;
        } else if (Constant.ENV_DEV2.equals(env)) {
            mBaseUrl = Constant.URL_BASE_DEV2;
        } else if (Constant.ENV_CIT2.equals(env)) {
            mBaseUrl = Constant.URL_BASE_CIT2;
        } else if (Constant.ENV_RELEASE.equals(env)) {
            mBaseUrl = Constant.URL_BASE_RELEASE;
        }

        final String newUrl = mBaseUrl + url;
        LogUtils.showLog("请求的地址：" + newUrl);
        final Request<String> request = NoHttp.createStringRequest(newUrl, RequestMethod.POST);
        //添加头信息
        String token = sp.getValue(Constant.sp_Token);

        //auth_token必须传，不能为""
        if (!TextUtils.isEmpty(token)) {
            request.addHeader("auth_token", token);
        } else {
            request.addHeader("auth_token", "10000");
        }
        request.addHeader("deviceType", "a");
        List<Binary> binaries = new ArrayList<>();
        if (fileMap != null && !fileMap.isEmpty()) {
            for (Integer key : fileMap.keySet()) {
                if (!fileMap.containsKey(key) || StringUtils.isNullString(fileMap.get(key))) {
                    continue;
                }
                File file = new File(fileMap.get(key));
                FileBinary binary = new FileBinary(file);
                binary.setUploadListener(key, mOnUploadListener);
                binaries.add(binary);
            }
        }
        request.add("file", binaries);

        CallServer.getRequestInstance().add(what, request, new HttpListener<String>() {
            @Override
            public void onSucceed(int what, Response<String> response) {
                if ((response.getHeaders().getResponseCode() + "").startsWith("2")) {
                    String resultString = response.get();

                    LogUtils.showLog("请求的结果：" + resultString);

                    uploadBean = JSON.parseObject(resultString, FileUploadBean.class);

                    if ("4".equals(uploadBean.getCode())) {

                        return;
                    }

                    if (onFileUploadResultListener != null) {
                        onFileUploadResultListener.onSucceed(what, uploadBean);
                    }
                } else {
                    uploadBean = JSON.parseObject(response.get(), FileUploadBean.class);
                    if (onFileUploadResultListener != null) {
                        onFileUploadResultListener.onFailed(what, uploadBean);
                    }
                }
            }

            @Override
            public void onFailed(int what, Response<String> response) {
                String resultString = response.get();
                LogUtils.showLog("请求的结果：" + resultString);
                if (onFileUploadResultListener != null) {
                    uploadBean = JSON.parseObject(response.get(), FileUploadBean.class);
                    onFileUploadResultListener.onFailed(what, uploadBean);
                }
            }
        });
        return binaries;
    }

    public static List<Binary> httpUploadImgRequest(int what, String url, Map<Integer, String> fileMap, OnFileUploadListener onFileUploadListener, final OnFileUploadResultListener onFileUploadResultListener) {
        mOnFileUploadListener = onFileUploadListener;
        // 获取当前环境， 如果没有就赋值为默认环境， 里面的sp获取默认环境，如果没有就默认cit，默认环境在Application里初始化
        String env = sp.getValue(Constant.sp_Env, sp.getValue(Constant.sp_EnvDefault, Constant.ENV_DEFAULT));

        if (Constant.ENV_DEV.equals(env)) {
            mBaseUrl = Constant.URL_BASE_DEV;
        } else if (Constant.ENV_CIT.equals(env)) {
            mBaseUrl = Constant.URL_BASE_CIT;
        } else if (Constant.ENV_SIT.equals(env)) {
            mBaseUrl = Constant.URL_BASE_SIT;
        } else if (Constant.ENV_UAT.equals(env)) {
            mBaseUrl = Constant.URL_BASE_UAT;
        } else if (Constant.ENV_DEV2.equals(env)) {
            mBaseUrl = Constant.URL_BASE_DEV2;
        } else if (Constant.ENV_CIT2.equals(env)) {
            mBaseUrl = Constant.URL_BASE_CIT2;
        } else if (Constant.ENV_RELEASE.equals(env)) {
            mBaseUrl = Constant.URL_BASE_RELEASE;
        }

        final String newUrl = mBaseUrl + url;
        LogUtils.showLog("请求的地址：" + newUrl);
        final Request<String> request = NoHttp.createStringRequest(newUrl, RequestMethod.POST);
        //添加头信息
        String token = sp.getValue(Constant.sp_Token);

        //auth_token必须传，不能为""
        if (!TextUtils.isEmpty(token)) {
            request.addHeader("auth_token", token);
        } else {
            request.addHeader("auth_token", "10000");
        }
        request.addHeader("deviceType", "a");
        List<Binary> binaries = new ArrayList<>();
        if (fileMap != null && !fileMap.isEmpty()) {
            for (Integer key : fileMap.keySet()) {
                if (!fileMap.containsKey(key) || StringUtils.isNullString(fileMap.get(key))) {
                    continue;
                }
                fileMap.put(key, BitmapUtil.getSmallImage(fileMap.get(key)));
                File file = new File(fileMap.get(key));
                FileBinary binary = new FileBinary(file);
                binary.setUploadListener(key, mOnUploadListener);
                binaries.add(binary);
            }
        }
        request.add("file", binaries);

        CallServer.getRequestInstance().add(what, request, new HttpListener<String>() {
            @Override
            public void onSucceed(int what, Response<String> response) {
                if ((response.getHeaders().getResponseCode() + "").startsWith("2")) {
                    String resultString = response.get();

                    LogUtils.showLog("请求的结果：" + resultString);

                    uploadBean = JSON.parseObject(resultString, FileUploadBean.class);

                    if ("4".equals(uploadBean.getCode())) {

                        return;
                    }

                    if (onFileUploadResultListener != null) {
                        onFileUploadResultListener.onSucceed(what, uploadBean);
                    }
                } else {
                    uploadBean = JSON.parseObject(response.get(), FileUploadBean.class);
                    if (onFileUploadResultListener != null) {
                        onFileUploadResultListener.onFailed(what, uploadBean);
                    }
                }
            }

            @Override
            public void onFailed(int what, Response<String> response) {
                String resultString = response.get();
                LogUtils.showLog("请求的结果：" + resultString);
                if (onFileUploadResultListener != null) {
                    uploadBean = JSON.parseObject(response.get(), FileUploadBean.class);
                    onFileUploadResultListener.onFailed(what, uploadBean);
                }
            }
        });
        return binaries;
    }


    private static OnUploadListener mOnUploadListener = new OnUploadListener() {

        @Override
        public void onStart(int position) {    // 这个文件开始上传
            if (mOnFileUploadListener != null) {
                mOnFileUploadListener.onStart(position);
            }
        }

        @Override
        public void onCancel(int position) {    // 这个文件的上传被取消时
            if (mOnFileUploadListener != null) {
                mOnFileUploadListener.onCancel(position);
            }
        }

        @Override
        public void onProgress(int position, int progress) {    // 这个文件的上传进度发生变化
            if (mOnFileUploadListener != null) {
                mOnFileUploadListener.onProgress(position, progress);
            }
        }

        @Override
        public void onFinish(int position) {    // 文件上传完成
            if (mOnFileUploadListener != null) {
                mOnFileUploadListener.onFinish(position);
            }
        }

        @Override
        public void onError(int position, Exception exception) {    // 文件上传发生错误
            if (mOnFileUploadListener != null) {
                mOnFileUploadListener.onError(position, exception);
            }
        }
    };

    // 下载一个文件
    public static void httpDownloadOneFile(String url, String savePath, String fileName, boolean isRange, boolean isDeleteOld, final OnFileDownloadListener onFileDownloadListener) {
        // url 下载地址。
        // fileFolder 保存的文件夹。
        // fileName 文件名, 如果是断点续传就必须指定
        // isRange 是否断点续传下载。
        // isDeleteOld 如果发现存在同名文件，是否删除后重新下载，如果不删除，则直接下载成功。
        DownloadRequest mDownloadRequest = NoHttp.createDownloadRequest(url, savePath, fileName, isRange, isDeleteOld);

        // what 区分下载。
        // downloadRequest 下载请求对象。
        // downloadListener 下载监听。
        CallServer.getDownloadInstance().add(0, mDownloadRequest, new DownloadListener() {

            @Override
            public void onStart(int what, boolean isResume, long beforeLength, Headers headers, long allCount) {
                if (onFileDownloadListener != null) {
                    onFileDownloadListener.onStart(what, isResume, beforeLength, headers, allCount);
                }
            }

            @Override
            public void onDownloadError(int what, Exception exception) {
                if (onFileDownloadListener != null) {
                    onFileDownloadListener.onDownloadError(what, exception);
                }
            }

            @Override
            public void onProgress(int what, int progress, long fileCount) {
                if (onFileDownloadListener != null) {
                    onFileDownloadListener.onProgress(what, progress, fileCount);
                }
            }

            @Override
            public void onFinish(int what, String filePath) {
                if (onFileDownloadListener != null) {
                    onFileDownloadListener.onFinish(what, filePath);
                }
            }

            @Override
            public void onCancel(int what) {
                if (onFileDownloadListener != null) {
                    onFileDownloadListener.onCancel(what);
                }
            }
        });
    }
}
